<template>
  <div>
    <i-dialog
      :title="'添加设备'"
      :visible="visible"
      width="642px"
      :close-on-click-modal="false"
      @close="outerClose"
    >
      <i-form
        :items="formItems"
        :model="form"
        ref="form"
        :rules="rules"
        label-width="95px"
        class="demo-form"
      >
      </i-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import { addDeviceHistoryRemark } from '@/api/device'
import formValidate from '@/utils/validate'
export default {
  name: 'deviceHistoryRemark',
  data() {
    return {
      form: { deviceRemark: this.deviceAttr.deviceRemark },
      formItems: [
        {
          type: 'textarea',
          value: 'deviceRemark',
          label: '备注',
          clearable: true,
          maxlength: 20,
          rows: 4,
          showWordLimit: true,
        },
      ],
      rules: {
        deviceRemark: [
          {
            validator: formValidate('validateExplanation'),
            trigger: 'change blur',
          },
        ],
      },
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    deviceAttr: {
      type: Object,
      default: () => {},
    },
  },
  methods: {
    outerClose() {
      this.form = {}
      this.deviceAttr = {}
      this.$emit('update:visible', false)
    },
    refreshTable() {
      this.$emit('refreshTable')
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = {
            objId: this.deviceAttr.objId,
            ...this.form,
          }
          addDeviceHistoryRemark(params).then(
            (res) => {
              res
                ? this.$message({
                    message: '添加备注成功',
                    type: 'success',
                  })
                : this.$message({
                    message: '添加备注失败',
                    type: 'error',
                  })
              this.refreshTable()
              this.outerClose()
            },
            () => {
              this.refreshTable()
              this.outerClose()
            }
          )
        }
      })
    },
  },
}
</script>
